﻿using System;
using System.Web;
using DingHeng.TransportationManagement.Entity;


namespace DingHeng.TransportationManagement.WebCommon
{
    /// <summary>
    ///将所有QueryString  Session Cache操作在此类中完成 
    /// </summary>
    public class WebContext
    {

        /// <summary>
        /// Item Key
        /// </summary>
        public static readonly string WDCONTEXTKEY = "Current_WebContext"; //保存至items中值

        /// <summary>
        /// Query Key
        /// </summary>
        public static readonly string QUERYSTRING_DID = "id"; //Request中的did

        public static readonly string QUERYSTRING_FAKEUSER = "fakeuser";

        /// <summary>
        /// SESSION KEY
        /// </summary>
        public static readonly string SESSION_CURRSTAFF = "Session$CurrStaff";
        public static readonly string SESSION_ISSUPERADMIN = "Session$IsSuperAdmin";

        public HttpContext Context { get; set; }

        public WebContext() { Context = HttpContext.Current; }
        public WebContext(HttpContext context) { this.Context = context; }

        //返回当前上下文
        public static WebContext Current
        {
            get
            {
                WebContext wd = new WebContext();
                if (HttpContext.Current != null)
                {
                    if (HttpContext.Current.Items[WDCONTEXTKEY] != null)
                        wd = HttpContext.Current.Items[WDCONTEXTKEY] as WebContext;
                    else
                    {
                        HttpContext.Current.Items.Add(WDCONTEXTKEY, wd);
                    }
                }
                return wd;
            }
        }

        private int _flowId;
        /// <summary>
        /// 返回请求中的FlowId
        /// </summary>
        public int FlowId
        {
            get
            {
                int tempout;
                if (int.TryParse(Context.Request.QueryString[QUERYSTRING_DID], out tempout)) _flowId = tempout;

                return _flowId;
            }
        }


        /// <summary>
        /// 当前的切换用户
        /// </summary>
        public User CurrentUser
        {
            get { return (User)Context.Session[SESSION_CURRSTAFF]; }
            set { Context.Session[SESSION_CURRSTAFF] = value; }
        }


        /// <summary>
        /// 是否为超级管理员
        /// </summary>
        public bool IsSuperAdmin
        {
             get { return Context.Session[SESSION_ISSUPERADMIN] == null ? false : (bool)Context.Session[SESSION_ISSUPERADMIN] ; }
            set { Context.Session[SESSION_ISSUPERADMIN] = value; }
        }

    }
}